import { formatTime2, resizeImage } from '@/utils'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    filterFormatTime2(date) {
      return formatTime2(date, '{m}-{d} {h}:{i}')
    }
  },
  data() {
    return {
      tableKey: 0,
      total: 0,
      pageLoading: false,
      usersOptions: [],
      weightOptions: [
        {
          id: 0.5,
          name: '工时1天以内'
        },
        {
          id: 1,
          name: '工时1～2天'
        },
        {
          id: 3,
          name: '工时3～5天'
        },
        {
          id: 6,
          name: '工时6～10天'
        }
      ],
      priorityOptions: [
        {
          id: 1,
          name: '非常高'
        },
        {
          id: 2,
          name: '高'
        },
        {
          id: 3,
          name: '一般'
        },
        {
          id: 4,
          name: '低'
        }
      ],
      statusOptions: [
        {
          id: 1,
          name: '未开始'
        },
        {
          id: 2,
          name: '进行中'
        },
        {
          id: 3,
          name: '已完成'
        },
        {
          id: 4,
          name: '取消'
        }
      ],
      reviewStatusOptions: [
        {
          id: 0,
          name: '未核查'
        },
        {
          id: 1,
          name: '已核查'
        }
      ],
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        project_id: ''
      },
      listData: [],
      temp: {
        name: '',
        intro: '',
        weight: '',
        status: 1,
        progress: 0,
        priority: 3,
        review_by: '',
        execute_by: [],
        review_status: 0,
        score: 0,
        start_at: '',
        created_at: '',
        complete_at: '',
        deadline_at: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      }
    }
  },
  computed: {
    uuName() {
      return this.$store.state.user.userInfo.nickname
    }
  },
  methods: {
    validateStartEndTime(temp) {
      if (!temp.start_at || !temp.deadline_at) return true
      // Safari 日期格式处理
      if (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)) {
        return new Date(temp.start_at.replaceAll(/-/g, '/')).getTime() < new Date(temp.deadline_at.replaceAll(/-/g, '/')).getTime()
      }
      return new Date(temp.start_at).getTime() < new Date(temp.deadline_at).getTime()
    },
    tableRowClassName({ row }) {
      if (row.status === 4) {
        return 'grey-row'
      } else if (row.status === 3 || row.progress === 100) {
        if (new Date(row.deadline_at).getTime() - new Date(row.complete_at).getTime() < 0) {
          return 'not-done-row'
        }
        return 'done-row'
      } else if (new Date(row.deadline_at).getTime() - new Date().getTime() < 0) {
        return 'not-done-row'
      }
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleStatusProgress(val) {
      this.$forceUpdate()
      switch (val) {
        case 3:
          this.temp['progress'] = 100
          break
        default:
          this.temp['progress'] = 0
          break
      }
    },
    curSorMoveEnd() {
      setTimeout(() => {
        window.getSelection()
        const range = document.createRange()
        range.selectNodeContents(this.$refs.introTextarea)
        range.collapse(false)
        const sel = window.getSelection()
        sel.removeAllRanges()
        sel.addRange(range)
      }, 0)
    },
    handlePaste(e) {
      const file = e.clipboardData.files[0]
      if (file && file.type.startsWith('image')) {
        e.preventDefault()
        resizeImage(file).then(res => {
          this.$refs.introTextarea.innerHTML += `<img style="max-width: 100%;cursor: pointer" src="${res}" />`
          this.curSorMoveEnd()
        })
      }
    },
    resetTemp() {
      this.temp = {
        name: '',
        intro: '',
        weight: '',
        status: 1,
        priority: 3,
        progress: 0,
        score: 0,
        review_status: 0,
        review_by: this.head.nickname,
        execute_by: [],
        project_id: this.$route.params.id,
        deadline_at: '',
        created_at: '',
        complete_at: '',
        start_at: ''
      }
      setTimeout(() => {
        if (this.$refs.introTextarea) {
          this.$refs.introTextarea.innerHTML = ''
        }
      }, 0)
    }
  }
}
